<?php
namespace App\Blocks;

use App\Models\Enroll;
use App\Models\EnrollPersonal;
use DB;
class Activity
{
    public function enroll($enrollData, $personalData){
        $enrollModel = new Enroll();
        $enrollPersonalModel = new EnrollPersonal();
        $activityModel = new \App\Models\Activity();
        $userModel = new \App\Models\User();

        $enrollPersonalNum = count($personalData);

        $activity = DB::table($activityModel->getTable())
            ->where('id', $enrollData['activity_id'])
            ->where('surplus_number', '>=', $enrollPersonalNum)
            ->whereNull('deleted_at')
            ->first();

        if(DB::table($enrollModel->getTable())->where('activity_id', $enrollData['activity_id'])->where('u_id', $enrollData['u_id'])->whereNull('deleted_at')->first()){
            return false;
        }

        if(!$activity){
            return false;
        }

        $integral = 0;

        try{
            DB::beginTransaction();
            $enrollId = DB::table($enrollModel->getTable())->insertGetId($enrollData);

            if(!$enrollId){
                DB::rollBack();
                \Log::error('添加报名信息错误');
                return false;
            }

            $enrollPersonal = [];

            foreach ($personalData as  $p){
                $integral += $activity->cost;
                $enrollPersonal[] = [
                    'enroll_id' => $enrollId,
                    'personal_id' => $p
                ];
            }
            if(
                !DB::table($enrollPersonalModel->getTable())->insert($enrollPersonal)
            ){
                DB::rollBack();
                \Log::error('添加报名联系人信息错误');
                return false;
            }

            if(!DB::table($activityModel->getTable())
                ->where('id', $enrollData['activity_id'])
                ->decrement('surplus_number', $enrollPersonalNum)
            ){
                DB::rollBack();
                \Log::error('减少人数失败');
                return false;
            }

            if(
                DB::table($userModel->getTable())->where('id', $enrollData['u_id'])
                ->increment('integral', $integral) === false
            ){
                DB::rollBack();
                \Log::error('修改用户积分失败');
                return false;
            }

            DB::commit();
            return true;
        }catch (\Exception $exception){
            DB::rollBack();
            \Log::error($exception);
            return false;
        }
    }

    public function enrollActivity($search = [], $page = 1, $length = 20){
        $enrollModel = new Enroll();
        $activityModel = new \App\Models\Activity();

        $sql = DB::table($enrollModel->getTable() . ' as e')
            ->leftJoin($activityModel->getTable() . ' as a', 'e.activity_id', '=', 'a.id')
            ->whereNull('e.deleted_at');

        if(isset($search['u_id'])){
            $sql->where('e.u_id', $search['u_id']);
        }

        if(isset($search['off_line_time'])){
            $sql->where('a.off_line_time', '>=', date('Y-m-d H:i:s', time()));
        }

        $enrollList['pageData']['count']     = $sql->count();
        $enrollList['pageData']['lastPage']  = ceil($enrollList['pageData']['count'] / $length);

        $enrollList['list'] = $sql->orderBy('e.created_at', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $enrollList;

    }
}